﻿namespace TapMe.View
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Diagnostics;

    using TapMe.ViewModel;

    public partial class GamePlayPage : PhoneApplicationPage
    {

        private const string MAIN_PAGE = "/View/MainPage.xaml";

        private int numFingers;
        private bool hasStarted;
        private bool isActive;

        public GamePlayPage()
        {
            InitializeComponent();
            Touch.FrameReported += new TouchFrameEventHandler(HandleTouch);
            this.hasStarted = false;
            this.numFingers = -1;
            this.isActive = false;
            this.DataContext = new ViewModel();            
        }

        private void HandleTouch(object sender, TouchFrameEventArgs e)
        {
            if (isActive)
            {
                this.numFingers = e.GetTouchPoints(ContentPanel).Count;

                if (this.numFingers == 1 && hasStarted)
                {
                    ContentPanel.Fill = new SolidColorBrush(Colors.Cyan);
                    ContentPanel.Opacity = 100;
                }
                else if (this.numFingers == 2 && hasStarted)
                {
                    ContentPanel.Fill = new SolidColorBrush(Colors.Purple);
                    ContentPanel.Opacity = 100;
                }
                else if (this.numFingers == 3 && hasStarted)
                {
                    ContentPanel.Fill = new SolidColorBrush(Colors.Green);
                    ContentPanel.Opacity = 100;
                }
            }        
        }

        private void HandleTap(object sender, System.Windows.Input.GestureEventArgs e) {
            //MessageBox.Show("ell");
        }

        private void HandleHold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            MessageBox.Show("Hola!");
        }

        private void HandleManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            Debug.WriteLine("Started");
            this.hasStarted = true;
        }

        private void HandleManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
        }

        private void HandleManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            ContentPanel.Opacity = 0;
            this.numFingers = -1;
            this.hasStarted = false;
        }

        

        private void QuitButtonHandler(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "QUIT", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                NavigationService.Navigate(new Uri(MAIN_PAGE, UriKind.Relative));
            }
        }

        #region page navigation handler
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.isActive = true;
            if (NavigationService.CanGoBack)
            {
                NavigationService.RemoveBackEntry();
            }
            base.OnNavigatedFrom(e);
        }


        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.isActive = false;
            if (NavigationService.CanGoBack)
            {
                NavigationService.RemoveBackEntry();
            }
            base.OnNavigatedFrom(e);
        }
        #endregion
    }
}